package com.heima.comment.service.impl;

import com.heima.comment.service.CommentUserService;
import com.heima.model.comment.pojos.CommentUser;
import com.heima.utils.common.CommentUserThreadLocalUtil;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

/**
 * @author jay
 * @date 2022/11/17 21:58:48
 */
@Service
public class CommentUserServiceImpl implements CommentUserService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public boolean isStared(String commentId) {
        if(commentId == null){
            return false;
        }
        Query query = Query.query(Criteria.where("commentId").is(commentId));
        query.addCriteria(Criteria.where("userId").is(CommentUserThreadLocalUtil.get().getId()));
        return mongoTemplate.findOne(query, CommentUser.class) != null;
    }

    @Override
    public void save(String commentId) {
        // 1. 参数检测
        if(commentId == null){
            return;
        }

        CommentUser commentUser = new CommentUser();
        commentUser.setCommentId(commentId);
        commentUser.setUserId(CommentUserThreadLocalUtil.get().getId());
        mongoTemplate.save(commentUser);
    }

    @Override
    public void delete(String commentId) {
        if(commentId == null){
            return;
        }
        Query query = Query.query(Criteria.where("userId").is(CommentUserThreadLocalUtil.get().getId()));
        query.addCriteria(Criteria.where("commentId").is(commentId));
        mongoTemplate.remove(query, CommentUser.class);
    }


}
